/**
 * Create in 2012-7-28.
 */
package kit4j.tests.jdk.swing.dnd.factory;

import javax.swing.JLabel;

/**
 * 
 * @author MiniKnife
 *
 */
public class EmptyLabelFactory {
	
	static abstract class IdJLabel extends JLabel {

		private static final long serialVersionUID = 1L;
		
		public IdJLabel() {
			super();
			setOpaque(true);
		}
		
		public IdJLabel(String label) {
			super(label);
		}
		
		abstract public int getLabelId();
		
		@Override
		public boolean equals(Object obj) {
			if(obj instanceof IdJLabel) {
				IdJLabel other = (IdJLabel) obj;
				
				return getLabelId() == other.getLabelId();
			} else {
				return false;
			}
		}
	}
	
	private static int staticLabelId = 0;
	
	private static final Object idLockObject = new Object();
	
	public static JLabel createUniqueLabel() {
		final int id = generateLabelId();
		
		return new IdJLabel() {
			
			private static final long serialVersionUID = 1L;

			public int getLabelId() {
				return id;
			}
			
		};
	}
	
	public static JLabel[] createUniqueLabel(int count) {
		JLabel[] labels = new JLabel[count];
		
		for(int i = 0; i < count; ++i) {
			labels[i] = createUniqueLabel();
		}
		
		return labels;
	}
	
	private static int generateLabelId() {
		synchronized(idLockObject) {
			return staticLabelId++;
		}
	}
}
